package com.alex.space.commons.position;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.client.fluent.Content;
import org.apache.http.client.fluent.Request;

import static com.alex.space.commons.position.PositionOnlineUtils.TransformationConstant.*;


/**
 * Created by root on 2017/5/9/0009.
 * <p>
 * 坐标转换测试
 * 将project 转成百度坐标
 * 将地块转成百度坐标（误差）
 * </p>
 */
public class PositionOnlineUtils {

    private static ObjectMapper objectMapper;

    /**
     * 将项目转成百度坐标
     *
     * @param projectName 要转换的名
     * @param city        所在城市
     * @return lng\tlat
     */
    public static String toPosition(String projectName, String city) {
        objectMapper = new ObjectMapper();
        String position = "";
        String buildAddress = String.format(HTTPURL_BAIDU, projectName.replaceAll("[·]", "").trim(), AK_OWNER);
        if (StringUtils.isNoneBlank(city)) {
            buildAddress = buildAddress.concat("&city=" + city);
        }
        try {
            Content content = Request.Get(buildAddress).execute().returnContent();
            JsonNode jsonResult = objectMapper.readTree(content.asStream());
            if (jsonResult.findValue("status").asInt() == 0) {
                String lng = jsonResult.findValue("lng").asText();
                String lat = jsonResult.findValue("lat").asText();
                position = lng.concat("\t").concat(lat);
            }
        } catch (Exception e) {
            System.out.println(buildAddress);
        }
        return position;
    }

    /**
     * 地块坐标转换
     */
    public static String toArea(String areaName) {
        objectMapper = new ObjectMapper();
        String position = "";
        try {
            String filterName = areaName.replaceAll("([,;:，、-])|([0-9]+)|([a-zA-Z]+)|东至|南至|西至|北至", "");
            Content content = Request.Get(String.format(HTTPURL_BAIDU, filterName.trim(), AK_OWNER)).execute().returnContent();
            JsonNode jsonResult = objectMapper.readTree(content.asStream());
            if (jsonResult.findValue("status").asInt() == 0) {
                String lng = jsonResult.findValue("lng").asText();
                String lat = jsonResult.findValue("lat").asText();
                position = lng.concat("\t").concat(lat);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return position;
    }

    public static String WGS84_2_Bd09(String lng, String lat) {
        objectMapper = new ObjectMapper();
        String result = "";
        try {
            Content content = Request.Get(String.format(BAIDU_TRANSFER, lng + "," + lat, AK_OWNER)).execute().returnContent();
            JsonNode jsonResult = objectMapper.readTree(content.asStream());
            ArrayNode array = (ArrayNode) jsonResult.get("result");
            for (int i = 0; i < array.size(); i++) {
                JsonNode node = array.get(i);
                String newlng = node.findValue("x").asText();
                String newlat = node.findValue("y").asText();
                result = newlng + " " + newlat;
            }


        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    public interface TransformationConstant {

        /**
         * 百度坐标转换WebApi
         * 每天限量6000次
         */
        String HTTPURL_BAIDU = "http://api.map.baidu.com/geocoder/v2/?address=%s&output=json&ak=%s";

        /**
         * 坐标转百度坐标
         */
        String BAIDU_TRANSFER = "http://api.map.baidu.com/geoconv/v1/?coords=%s&from=1&to=5&ak=%s";

        /**
         * 密钥-请到百度开发者中心申请
         * <p>
         * 16E84FA43ff4e5c6768cf80da884472f
         * sIMOXLuQoxsriaOY3iNG9cV16cA0wjuE
         */
        String AK_OWNER = "mNi9owK792AVHxdH45WKs9HfINkODsRq";

        /**
         * 1：GPS设备获取的角度坐标，wgs84坐标;
         * <p>
         * 2：GPS获取的米制坐标、sogou地图所用坐标;
         * <p>
         * 3：google地图、soso地图、aliyun地图、mapabc地图和amap地图所用坐标，国测局坐标;
         * <p>
         * 4：3中列表地图坐标对应的米制坐标;
         * <p>
         * 5：百度地图采用的经纬度坐标;
         * <p>
         * 6：百度地图采用的米制坐标;
         * <p>
         * 7：mapbar地图坐标;
         * <p>
         * 8：51地图坐标
         */
        String FROM = "1";

        /**
         * 有两种可供选择：5、6。
         * <p>
         * 5：bd09ll(百度经纬度坐标),
         * <p>
         * 6：bd09mc(百度米制经纬度坐标);
         */
        String TO = "5";
    }

}
